package com.atguigu.serviceedu.service.impl;

import com.atguigu.servicebase.handler.GuliException;
import com.atguigu.serviceedu.client.VodClient;
import com.atguigu.serviceedu.entity.EduChapter;
import com.atguigu.serviceedu.entity.EduCourse;
import com.atguigu.serviceedu.entity.EduCourseDescription;
import com.atguigu.serviceedu.entity.EduVideo;
import com.atguigu.serviceedu.entity.vo.CourseInfoForm;
import com.atguigu.serviceedu.entity.vo.CoursePublishVo;
import com.atguigu.serviceedu.entity.vo.CourseQueryVo;
import com.atguigu.serviceedu.entity.vo.CourseWebVo;
import com.atguigu.serviceedu.mapper.EduCourseMapper;
import com.atguigu.serviceedu.service.EduChapterService;
import com.atguigu.serviceedu.service.EduCourseDescriptionService;
import com.atguigu.serviceedu.service.EduCourseService;
import com.atguigu.serviceedu.service.EduVideoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-11-24
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    private EduCourseDescriptionService courseDescriptionService;

    @Autowired
    private EduChapterService chapterService;

    @Autowired
    private EduVideoService videoService;

    @Autowired
    private VodClient vodClient;

    //添加课程信息
    @Override
    public String addCourseInfo(CourseInfoForm courseInfoForm) {
        //1添加课程信息
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoForm, eduCourse);
        int insert = baseMapper.insert(eduCourse);
        if (insert == 0) {
            throw new GuliException(20001, "创建课程失败");
        }
        //2获得课程id---只要插入成功主键id就生成了
        String courseId = eduCourse.getId();
        //3添加描述信息----因为使用工具类耗内存所有少的不使用
        EduCourseDescription courseDescription  = new EduCourseDescription();
        courseDescription .setId(courseId);
        courseDescription .setDescription(courseInfoForm.getDescription());
        courseDescriptionService.save(courseDescription);


        return courseId;
    }



    //根据id查询课程信息
    @Override
    public CourseInfoForm getCourseInfoById(String id) {
        //1根据id查询课程信息
        EduCourse eduCourse = baseMapper.selectById(id);
        //2封装课程信息
        CourseInfoForm courseInfoForm = new CourseInfoForm();
        BeanUtils.copyProperties(eduCourse, courseInfoForm);
        //3根据id查询课程描述
        EduCourseDescription courseDescription = courseDescriptionService.getById(id);
        //4封装课程描述
        courseInfoForm.setDescription(courseDescription.getDescription());
        return courseInfoForm;

    }
    //修改课程信息
    @Override
    public void updateCourseInfo(CourseInfoForm courseInfoForm) {
        //1.复制课程数据
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoForm, eduCourse);
        //2.更新课程数据
        int update = baseMapper.updateById(eduCourse);
        //3判断是否成功
        if (update == 0) {
            throw new GuliException(20001, "修改课程失败");
        }
        //4.更新课程描述
        EduCourseDescription courseDescription = new EduCourseDescription();
        courseDescription.setId(courseInfoForm.getId());
        courseDescription.setDescription(courseInfoForm.getDescription());
        courseDescriptionService.updateById(courseDescription);
    }

    @Override
    public CoursePublishVo getCoursePublishById(String id) {
        CoursePublishVo coursePublishVo = baseMapper.getCoursePublishById(id);
        return coursePublishVo;
    }
    //根据id删除课程相关信息
    @Override
    public void delCourseInfo(String id) {
        //1 删除视频
        //1.1查询满足条件的小节
        QueryWrapper<EduVideo> videoIdWrapper = new QueryWrapper<>();
        videoIdWrapper.eq("course_id", id);
        List<EduVideo> videoList = videoService.list(videoIdWrapper);
        //1.2判断是否为空
        if (videoList != null) {
            //1.3遍历封装
            //2、删除小节
            List<String> videoIdList = new ArrayList<>();
            for (int i = 0; i < videoList.size(); i++) {
                EduVideo eduVideo = videoList.get(i);
                videoIdList.add(eduVideo.getVideoSourceId());
            }
            //1.4远程调用
            if (videoIdList.size() > 0) {
                vodClient.deleteVideoList(videoIdList);
            }
        }
        //2、删除小节
        QueryWrapper<EduVideo> videoWrapper = new QueryWrapper<>();
        videoWrapper.eq("course_id",id);
        videoService.remove(videoWrapper);
        //3、删除章节
        QueryWrapper<EduChapter> chapterWrapper = new QueryWrapper<>();
        chapterWrapper.eq("course_id",id);
        chapterService.remove(chapterWrapper);
        //4、删除描述
        courseDescriptionService.removeById(id);

        //5、删除课程
        int delete = baseMapper.deleteById(id);
        if(delete==0){
            throw new GuliException(20001,"删除课程失败");
        }
    }

    //带条件分页
    @Override
    public void pageQuery(Page<EduCourse> pageParam, CourseQueryVo courseQueryVo) {
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("gmt_create");

        if (courseQueryVo == null) {
            baseMapper.selectPage(pageParam, queryWrapper);
            return;
        }
        String title = courseQueryVo.getTitle();
        String teacherId = courseQueryVo.getTeacherId();
        String subjectParentId = courseQueryVo.getSubjectParentId();
        String subjectId = courseQueryVo.getSubjectId();

        if (!StringUtils.isEmpty(title)) {
            queryWrapper.eq("title", title);
        }

        if (!StringUtils.isEmpty(teacherId)) {
            queryWrapper.eq("teacher_id", teacherId);
        }

        if (!StringUtils.isEmpty(subjectId)) {
            queryWrapper.eq("subject_parent_id", subjectParentId);
        }

        if (!StringUtils.isEmpty(subjectParentId)) {
            queryWrapper.eq("subject_id", subjectId);
        }

        baseMapper.selectPage(pageParam, queryWrapper);
    }

    //带条件分页查询课程列表
    @Override
    public Map<String, Object> getCourseApiPageVo(Page<EduCourse> pageParam, CourseQueryVo courseQueryVo) {
        //1取出查询条件
        String subjectParentId = courseQueryVo.getSubjectParentId();//一级类别id
        String subjectId = courseQueryVo.getSubjectId();//二级类别id
        String buyCountSort = courseQueryVo.getBuyCountSort();//销量排序
        String gmtCreateSort = courseQueryVo.getGmtCreateSort();//最新时间排序
        String priceSort = courseQueryVo.getPriceSort();//价格排序
        //2 判断是否为空，不为空拼sql
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(subjectParentId)) {
            wrapper.eq("subject_parent_id", subjectParentId);
        }
        if(!StringUtils.isEmpty(subjectId)){
            wrapper.eq("subject_id",subjectId);
        }
        if(!StringUtils.isEmpty(buyCountSort)){
            wrapper.orderByDesc("buy_count");
        }
        if(!StringUtils.isEmpty(gmtCreateSort)){
            wrapper.orderByDesc("gmt_create");
        }
        if(!StringUtils.isEmpty(priceSort)){
            wrapper.orderByDesc("price");
        }
        //3 分页查询
        baseMapper.selectPage(pageParam, wrapper);
        //4 查询结果封装map
        List<EduCourse> records = pageParam.getRecords();
        long current = pageParam.getCurrent();
        long pages = pageParam.getPages();
        long size = pageParam.getSize();
        long total = pageParam.getTotal();
        boolean hasNext = pageParam.hasNext();
        boolean hasPrevious = pageParam.hasPrevious();

        HashMap<String, Object> map = new HashMap<>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);
//map返回
        return map;
    }

    @Override
    public CourseWebVo getCourseApiInfo(String id) {
         CourseWebVo courseWebVo = baseMapper.getCourseApiInfo(id);
        return courseWebVo;
    }


}

